﻿
using System.Collections.Generic;
using System.Data;
using System.Text;
using Channelvn.Cached;
using Core.Library;
using System.Web;
using DataObject.Ref;

namespace DataObject.Members
{
    public class MembersController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static void Members_Update(MembersObject o)
        {
            MembersDAL.Members_Update(o);
        }

        public static void Members_Update_IsSlide(int id)
        {
            MembersDAL.Members_Update_IsSlide(id);
        }

        public static void Members_Update_IsHomePage(int id)
        {
            MembersDAL.Members_Update_IsHomePage(id);
        }

        public static void Members_Update_Status(int id, int status, int round)
        {
            MembersDAL.Members_Update_Status(id, status, round);
        }

        public static void Members_Update_Vote_Cheat(int id, int round, int vote, short cheat)
        {
            MembersDAL.Members_Update_Vote_Cheat(id, round, vote, cheat);
        }

        public static void Members_Update_AntiClone(int id, bool isClone, short cloneLevel, bool isLockVote)
        {
            MembersDAL.Members_Update_AntiClone(id, isClone, cloneLevel, isLockVote);
        }

        public static MembersObject Members_GetById(int id)
        {
            return ObjectHelper.MapRecord<MembersObject>(MembersDAL.Members_GetById(id));
        }

        public static int Members_Count(string kName, string kEmail, int inRound, int status)
        {
            return MembersDAL.Members_Count(kName, kEmail, inRound, status);
        }

        public static int Members_GetStatistic(int inRound, int status)
        {
            return MembersDAL.Members_GetStatistic(inRound, status);
        }

        public static List<MembersObject> Members_GetListByTypeByStatusPaging(string kName, string kEmail, int inRound, int status, int pageIndex, int pageSize, string order)
        {
            return ObjectHelper.MapRecords<MembersObject>(MembersDAL.Members_GetListByTypeByStatusPaging(kName, kEmail, inRound, status, pageIndex, pageSize, order));
        }

        public static List<CityObject> City_GetAll()
        {
            return ObjectHelper.MapRecords<CityObject>(MembersDAL.City_GetAll());
        }

        /***************************************
        = Front End
        -------------------------------------- */


        public static string Member_HighlightHomePage_BoxNhung(int top, int round, int _imageWidth, int _imageHeight)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Member_HighlightHomePage_Random(top, (int)MemberStatus.IsPublished, round);
            }
            var temp = @"<div id=""boxnhung"" class=""{1}""><div class=""boxnhung-header""><a href=""http://bestylist.kenh14.vn"" class=""boxnhung-name""></a><a class=""dtcs-pre"" href=""javascript:void(0);""></a><a class=""dtcs-next"" href=""javascript:void(0);""></a></div><ul>{0}</ul></div>";
            var sb = new StringBuilder();
            var cls = "nodisplay";
            if (dt != null && dt.Rows.Count >= 4)
            {
                var li = @"<li> <a></a> <a target=""_blank"" class=""boxnhung-img"" href=""{2}"" title=""{1}"">{3}</a> {4} <a  style=""display:none"" target=""_blank"" class=""boxnhung-title"" href=""{2}"" title=""{1}"">{0}</a></li>";
                var vote =
                    @"<a href=""{1}"" target=""_blank"" title=""{0}"" class=""boxnhung-vote"" style=""display:none""><p>{2}</p></a>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    var avatar = Utils.Photo.BuildPhoto(
                    source: r[Constants.Table_Members.Member_Avatar].ToString(),
                    title: r[Constants.Table_Members.Member_FullName].ToString(),
                    width: _imageWidth,
                    height: _imageHeight,
                    mustThumb: true);
                    var url = UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    sb.AppendFormat(li, r[Constants.Table_Members.Member_FullName]
                                    , UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName])
                                    , url
                                    , avatar, RefCache.Ref_GetVoteDisplayStatus() ? string.Format(vote, r[Constants.Table_Members.Member_FullName]
                                    , url
                                    , string.Format("{0} VOTE", r[Constants.Table_Members.Vote])) : string.Empty);
                }
                cls = string.Empty;
            }

            return string.Format(temp, sb, cls);
        }

        public static DataTable Member_HighlightHomePage(int top, int round)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Member_HighlightHomePage(top, (int)MemberStatus.IsPublished, round);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                }
            }
            return dt;
        }

        public static MembersObject Members_GetByEmail(string email)
        {
            return ObjectHelper.MapRecord<MembersObject>(MembersDAL.Members_GetByEmail(email));
        }

        public static MembersObject Members_GetByIdStore(int id)
        {
            return ObjectHelper.MapRecord<MembersObject>(MembersDAL.Members_GetByIdStore(id));
        }

        public static int Members_Insert(MembersObject o)
        {
            return MembersDAL.Members_Insert(o);
        }

        internal static DataTable City_GetAllFrontEnd()
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.City_GetAll();
            }
            return dt;
        }

        public static void Members_UpdateFrontEnd(MembersObject o)
        {
            MembersDAL.Members_Update(o);
            var listParams = new object[] { o.Member_Email };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Members_GetByEmail", listParams);
            CacheController.Remove(Constants.USER_CACHE_CATEGORY, cacheName);
            listParams = new object[] { o.Member_InRound, o.Member_Email };
            cacheName = CacheHelper.CacheKey.GetCacheKey("Members_GetByEmailFrontEnd", listParams);
            CacheController.Remove(Constants.USER_CACHE_CATEGORY, cacheName);
            listParams = new object[] { o.Member_ID };
            cacheName = CacheHelper.CacheKey.GetCacheKey("Members_GetByIdStore", listParams);
            CacheController.Remove(Constants.USER_CACHE_CATEGORY, cacheName);
            listParams = new object[] { o.Member_InRound, o.Member_ID };
            cacheName = CacheHelper.CacheKey.GetCacheKey("Members_GetByIdFrontEnd", listParams);
            CacheController.Remove(Constants.USER_CACHE_CATEGORY, cacheName);
        }

        public static DataTable Members_GetList_Prev2(int round, string keyword, int pageIndex, int pageSize)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_GetList_Prev2((int)MemberStatus.IsPublished, round, keyword, pageIndex, pageSize);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                }
            }
            return dt;
        }

        public static DataTable Members_GetList(int round, int ordertype, string keyword, int pageIndex, int pageSize)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                var orderstring = string.Empty;
                switch (ordertype)
                {
                    case 1:
                        orderstring = Constants.Table_Members.Member_RegisterDate + " DESC";
                        break;
                    case 2:
                        orderstring = Constants.Table_Members.Vote + round.ToString() + " DESC";
                        break;
                    case 3:
                        orderstring = Constants.Table_Members.Member_DisplayName + " ASC";
                        break;
                    default:
                        orderstring = Constants.Table_Members.Member_RegisterDate + " DESC";
                        break;

                }
                dt = db.StoredProcedures.Members_GetList((int)MemberStatus.IsPublished, round, orderstring, keyword, pageIndex, pageSize);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                }
            }
            return dt;
        }

        internal static int Members_GetList_Count_Prev2(int round, string keyword)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_GetList_Count_Prev2((int)MemberStatus.IsPublished, round, keyword);
            }

            if (dt != null && dt.Rows.Count > 0)
                return ConvertUtility.ConvertTo<int>(dt.Rows[0][0]);
            return 0;
        }

        internal static int Members_GetList_Count(int round, int ordertype, string keyword)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_GetList_Count((int)MemberStatus.IsPublished, round, ordertype, keyword);
            }

            if (dt != null && dt.Rows.Count > 0)
                return ConvertUtility.ConvertTo<int>(dt.Rows[0][0]);
            return 0;
        }

        public static DataTable Members_GetByEmailFrontEnd(int round, string email)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_GetByEmailFrontEnd((int)MemberStatus.IsPublished, round, email);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodedBase64Title);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodedBase64Title] =
                            SecurityUtility.Encrypt.EncodeTo64(r[Constants.Table_Members.Member_FullName].ToString());

                }
            }
            return dt;
        }

        public static DataTable Members_GetByIdFrontEnd(int round, int id)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_GetByIdFrontEnd((int)MemberStatus.IsPublished, round, id);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.Age);
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodedBase64Title);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.Age] =
                            DateTimeFunctions.CountAge(r[Constants.Table_Members.Member_Birthday]);
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodedBase64Title] =
                            SecurityUtility.Encrypt.EncodeTo64(HttpUtility.UrlEncode(r[Constants.Table_Members.Member_FullName].ToString()));

                }
            }
            return dt;
        }

        public static DataTable Members_InSlide(int top, int round)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Members_InSlide(top, (int)MemberStatus.IsPublished, round);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                dt.Columns.Add(Constants.Table_Members.Url);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);

                }
            }
            return dt;
        }
    }
}
